package servlets.games;

import hibernate.method.FeedbackMethods;
import hibernate.method.GameCategoryMethods;
import hibernate.method.GameMethods;
import hibernate.model.Game;
import hibernate.model.GameFeedback;
import hibernate.util.HibernateUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Servlet implementation class deleteGame
 */
@WebServlet("/deleteGame")
public class deleteGame extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public deleteGame() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String req = request.getParameter("game_id");
		int game_id = Integer.parseInt(req);
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/plain");
		SessionFactory sessionFactory = HibernateUtil
				.getSessionAnnotationFactory();
		Session s = sessionFactory.openSession();
		s.beginTransaction();
		GameMethods gm = new GameMethods();
		Game game = gm.idToGame(sessionFactory, s, game_id);
		
		JSONObject json = new JSONObject();
		PrintWriter out = response.getWriter();
		try {
			json.put("game_name", game.getName());
			json.put("add_date", game.getAdded());
			json.put("min_users", game.getUsers_min());
			json.put("max_users", game.getUsers_max());
			json.put("description", game.getDescribe());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		out.println(json);
		out.flush();

		s.getTransaction().commit();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		SessionFactory sessionFactory = HibernateUtil
				.getSessionAnnotationFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		String game = request.getParameter("game");
		int game_id = Integer.parseInt(game);
		
		GameMethods gm = new GameMethods();
		GameCategoryMethods gcat = new GameCategoryMethods();
		FeedbackMethods fm = new FeedbackMethods();
		Game g = gm.idToGame(sessionFactory, session, game_id);
		gm.delete(sessionFactory, session, g);
		gcat.deleteGameFromGamesCategories(sessionFactory, session, game_id);
		List<GameFeedback> gfb = new ArrayList<GameFeedback>(fm.listGameFeedbacks(sessionFactory, session, game_id));
		for (GameFeedback gfb1:gfb){
			fm.deleteGameFeedback(sessionFactory, session, gfb1);
		}
		
		/**TODO:  delete Rates*/
		
		try {
			session.getTransaction().commit();
			out.println("<script type=\"text/javascript\">alert(\"Usunieto gre\"); window.location.href = \"management/index.jsp?page=games&group=deleteGame\";</script>");
		} catch (Exception e) {
			out.println("<script type=\"text/javascript\">alert(\"Nie usunieto gry\"); window.location.href = \"managament/index.jsp?page=games&group=deleteGame\";</script>");
		}

	}

}
